#include "rotateDialog.h"

RotateDialog::RotateDialog(QList<QTreeWidgetItem *> _list, QWidget *_parent) : QDialog(_parent), list(_list){
    setWindowTitle(QString::fromUtf8("Rotação"));
    tetha = 0.0;
    rotateX = 1;
    rotateY = rotateZ = 0;
    relativePositionX = relativePositionY = relativePositionZ = 0;
    useRelativePosition = false;

    QLabel *rotateXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *rotateXDoubleSpinBox = new QDoubleSpinBox();
    rotateXDoubleSpinBox->setRange(-50.0, 50.0);
    rotateXDoubleSpinBox->setSingleStep(1.0);
    rotateXDoubleSpinBox->setValue(rotateX);
    connect(rotateXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRotateX(double)));


    QLabel *rotateYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *rotateYDoubleSpinBox = new QDoubleSpinBox();
    rotateYDoubleSpinBox->setRange(-50.0, 50.0);
    rotateYDoubleSpinBox->setSingleStep(1.0);
    rotateYDoubleSpinBox->setValue(rotateY);
    connect(rotateYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRotateY(double)));


    QLabel *rotateZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *rotateZDoubleSpinBox = new QDoubleSpinBox();
    rotateZDoubleSpinBox->setRange(-50.0, 50.0);
    rotateZDoubleSpinBox->setSingleStep(1.0);
    rotateZDoubleSpinBox->setValue(rotateZ);
    connect(rotateZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRotateZ(double)));


    QHBoxLayout *rotateLayout = new QHBoxLayout;
    rotateLayout->addWidget(rotateXLabel);
    rotateLayout->addWidget(rotateXDoubleSpinBox);
    rotateLayout->addStretch(1);
    rotateLayout->addWidget(rotateYLabel);
    rotateLayout->addWidget(rotateYDoubleSpinBox);
    rotateLayout->addStretch(1);
    rotateLayout->addWidget(rotateZLabel);
    rotateLayout->addWidget(rotateZDoubleSpinBox);
    rotateLayout->addStretch(1);

    QGroupBox *rotateGroupBox = new QGroupBox(QString::fromUtf8("Eixo de rotação"));
    rotateGroupBox->setLayout(rotateLayout);


    QDoubleSpinBox *tethaDoubleSpinBox = new QDoubleSpinBox();
    tethaDoubleSpinBox->setRange(-360.0, 360.0);
    tethaDoubleSpinBox->setSingleStep(30.0);
    tethaDoubleSpinBox->setValue(tetha);
    tethaDoubleSpinBox->setSuffix(QString::fromUtf8("°"));
    connect(tethaDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeTetha(double)));

    QHBoxLayout *tethaLayout = new QHBoxLayout;
//    tethaLayout->addWidget(tethaLabel);
    tethaLayout->addWidget(tethaDoubleSpinBox);
    tethaLayout->addStretch(1);

    QGroupBox *tethaGroupBox = new QGroupBox(QString::fromUtf8("Ângulo"));
    tethaGroupBox->setLayout(tethaLayout);



    QLabel *relativePositionXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *relativePositionXDoubleSpinBox = new QDoubleSpinBox();
    relativePositionXDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionXDoubleSpinBox->setSingleStep(1.0);
    relativePositionXDoubleSpinBox->setValue(relativePositionX);
    connect(relativePositionXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionX(double)));


    QLabel *relativePositionYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *relativePositionYDoubleSpinBox = new QDoubleSpinBox();
    relativePositionYDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionYDoubleSpinBox->setSingleStep(1.0);
    relativePositionYDoubleSpinBox->setValue(relativePositionY);
    connect(relativePositionYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionY(double)));


    QLabel *relativePositionZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *relativePositionZDoubleSpinBox = new QDoubleSpinBox();
    relativePositionZDoubleSpinBox->setRange(-50.0, 50.0);
    relativePositionZDoubleSpinBox->setSingleStep(1.0);
    relativePositionZDoubleSpinBox->setValue(relativePositionZ);
    connect(relativePositionZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRelativePositionZ(double)));


    QHBoxLayout *relativePositionLayout = new QHBoxLayout;
    relativePositionLayout->addWidget(relativePositionXLabel);
    relativePositionLayout->addWidget(relativePositionXDoubleSpinBox);
    relativePositionLayout->addStretch(1);
    relativePositionLayout->addWidget(relativePositionYLabel);
    relativePositionLayout->addWidget(relativePositionYDoubleSpinBox);
    relativePositionLayout->addStretch(1);
    relativePositionLayout->addWidget(relativePositionZLabel);
    relativePositionLayout->addWidget(relativePositionZDoubleSpinBox);
    relativePositionLayout->addStretch(1);


    QGroupBox *relativePositionGroupBox = new QGroupBox(QString::fromUtf8("Posição relativa"));
    relativePositionGroupBox->setLayout(relativePositionLayout);
    relativePositionGroupBox->setCheckable(true);
    relativePositionGroupBox->setChecked(useRelativePosition);
    connect(relativePositionGroupBox, SIGNAL(clicked(bool)), this, SLOT(changeUseRelativePosition(bool)));



    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(rotateGroupBox);
    mainLayout->addWidget(tethaGroupBox);
    mainLayout->addWidget(relativePositionGroupBox);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);

}

RotateDialog::~RotateDialog(){
}

void RotateDialog::acceptAndCreate(){
    if( (rotateX == 0) && (rotateY == 0) && (rotateZ == 0) ){
        QMessageBox::information(this, QString::fromUtf8("Eixo inválido"), QString::fromUtf8("Escolha uma direção válida."));
        return;
    }
    if(tetha == 0.0){
        accept();
    }

    Vector3D rotateVector(rotateX, rotateY, rotateZ);
    Vector3D relativePosition(relativePositionX, relativePositionY, relativePositionZ);
    for(int i = 0; i < list.size(); i++){
        QTreeWidgetItem *item = list.at(i);
        ((ObjectTreeItem *)item)->getObject()->rotate(tetha, rotateVector, relativePosition, useRelativePosition);
    }

    accept();
    done();
}

void RotateDialog::changeTetha(double _t){
    tetha = _t;
}

void RotateDialog::changeRotateX(double _x){
    rotateX = _x;
}

void RotateDialog::changeRotateY(double _y){
    rotateY = _y;
}

void RotateDialog::changeRotateZ(double _z){
    rotateZ = _z;
}

void RotateDialog::changeRelativePositionX(double _x){
    relativePositionX = _x;
}

void RotateDialog::changeRelativePositionY(double _y){
    relativePositionY = _y;
}

void RotateDialog::changeRelativePositionZ(double _z){
    relativePositionZ = _z;
}

void RotateDialog::changeUseRelativePosition(bool _b){
    useRelativePosition = _b;
}
